﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Tbl;
using System.Text;
using HKEA.CMS.Core.BLL.App;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_CPFollowInformation 的摘要说明
    /// </summary>
    public class M_CPFollowInformation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strRet = string.Empty;
            if (context.Request.QueryString["strID"] != null)
            {
                string[] infos = context.Request.QueryString["strID"].Split('_');
                if (infos.Length == 3)
                {
                    int uid = Convert.ToInt32(infos[0]);
                    string type = infos[1];
                    string adfrom=infos[2];
                    if (type == "cp")
                    {
                        strRet= GetAddressFromCP(uid,adfrom);
                    }
                    else if (type == "branch")
                    {
                        strRet = GetAddressFromBranch(uid, adfrom);
                    }
                    else
                    {
                        strRet = GetAddressFromCust(uid, adfrom);
                    }
                }
            }
            context.Response.Write(strRet);
            context.Response.Flush();
        }

        private string GetAddressFromCP(int UID, string From)
        {
            string strRet = string.Empty;
            if (From == "app")
            {
                app_tblContactPerson app_cpENT = new app_tblContactPerson();
                app_tblContactPersonBLL app_cpBLL = new app_tblContactPersonBLL();
                app_cpENT.AppContactPersonUID = UID;
                app_cpENT = app_cpBLL.GetEntity(app_cpENT);
                if (app_cpENT != null)
                {
                    if (app_cpENT.AppAddressUID > 0)
                    {
                        strRet = GetStrFromAppAddress((int)app_cpENT.AppAddressUID);
                    }
                    else if(app_cpENT.AddressUID > 0)
                    {
                        strRet = GetStrFromTblAddress(GettblAddressENT((int)app_cpENT.AddressUID));
                    }
                }
            }
            else
            {
                tblContactPerson cpENT = new tblContactPerson();
                cpENT.ContactPersonUID = UID;
                tblContactPersonBLL cpBLL = new tblContactPersonBLL();
                cpENT = cpBLL.GetEntity(cpENT);
                if (cpENT != null)
                {
                    strRet = GetStrFromTblAddress(cpENT.tblAddress);
                }
            }
            return strRet;
        }
        private string GetAddressFromBranch(int UID, string From)
        {
            string strRet = string.Empty;
            if (From == "app")
            {
                app_tblCust_Branch app_branchENT = new app_tblCust_Branch();
                app_branchENT.AppCustBranchUID = UID;
                app_branchENT.Deleted = 0;
                app_tblCust_BranchBLL app_branchBLL = new app_tblCust_BranchBLL();
                app_branchENT = app_branchBLL.GetListByENT(app_branchENT,true).FirstOrDefault();
                if (app_branchENT != null)
                {
                    if (app_branchENT.AppAddressUID > 0)
                    {
                        strRet = GetStrFromAppAddress((int)app_branchENT.AppAddressUID);
                    }
                    else if (app_branchENT.AddressUID > 0)
                    {
                        strRet = GetStrFromTblAddress(GettblAddressENT((int)app_branchENT.AddressUID));
                    }
                }
            }
            else
            {
                tblCust_Branch branchENT = new tblCust_Branch();
                tblCust_BranchBLL branchBLL = new tblCust_BranchBLL();
                branchENT.CustBranchUID = UID;
                branchENT = branchBLL.GetEntity(branchENT);
                strRet = GetStrFromTblAddress(GettblAddressENT((int)branchENT.AddressUID));
            }
            return strRet;
        }
        private string GetAddressFromCust(int UID, string From)
        {
            string strRet = string.Empty;
            if (From == "app")
            {
                app_tblCustomer app_customerENT = new app_tblCustomer();
                app_customerENT.AppCustomerUID = UID;
                app_tblCustomerBLL app_customerBLL = new app_tblCustomerBLL();
                app_customerENT = app_customerBLL.GetEntity(app_customerENT);
                if (app_customerENT != null)
                {
                    if (app_customerENT.AppAddressUID > 0)
                    {
                        strRet = GetStrFromAppAddress((int)app_customerENT.AppAddressUID);
                    }
                    else if (app_customerENT.AddressUID > 0)
                    { strRet = GetStrFromTblAddress(GettblAddressENT(app_customerENT.AddressUID)); }
                }
            }
            else
            {
                tblCustomer customerENT = new tblCustomer();
                customerENT.CustomerUID = UID;
                customerENT.Deleted = 0;
                tblCustomerBLL customerBLL = new tblCustomerBLL();
                customerENT = customerBLL.GetEntity(customerENT);
                if (customerENT != null)
                {
                    app_tblAddress app_addressENT = new app_tblAddress();
                    app_tblAddressBLL app_addressBLL = new app_tblAddressBLL();
                    app_addressENT.AddressUID = customerENT.AddressUID;
                    app_addressENT = app_addressBLL.GetAppENTByENT(app_addressENT);
                    if (app_addressENT != null) //通常情况下，页面修改数据将从关联表（或从基本表的关联字段）开始，并判断关联字段对应的App字段是否为空；
                    //Customer的Address Info可单独修改! 此为特殊情况,因此需要根据AddressUID 在App表中查找是否存在记录
                    { strRet = GetStrFromAppAddress(app_addressENT.AppAddressUID); }
                    else
                    { strRet = GetStrFromTblAddress(GettblAddressENT(customerENT.AddressUID)); }
                }
            }
            return strRet;
        }

        private string GetStrFromAppAddress(int appAddressUID)
        {
            StringBuilder strBuilder = new StringBuilder();
            app_tblAddress app_addressENT = new app_tblAddress();
            app_tblAddressBLL app_addressBLL = new app_tblAddressBLL();
            app_addressENT.AppAddressUID = appAddressUID;
            app_addressENT.Deleted = 0;
            app_addressENT = app_addressBLL.GetAppENTByENT(app_addressENT);
            
            if (app_addressENT != null)
            {
                strBuilder.Append("{");
                strBuilder.Append("\"Area_Chi\":\"" + app_addressENT.Area_Chi + "\",");
                strBuilder.Append("\"Area_Eng\":\"" + app_addressENT.Area_Eng + "\",");
                strBuilder.Append("\"City_Chi\":\"" + app_addressENT.City_Chi + "\",");
                strBuilder.Append("\"City_Eng\":\"" + app_addressENT.City_Eng + "\",");
                strBuilder.Append("\"CountryUID\":\"" + app_addressENT.CountryUID + "\",");
                strBuilder.Append("\"District_Chi\":\"" + app_addressENT.District_Chi + "\",");
                strBuilder.Append("\"District_Eng\":\"" + app_addressENT.District_Eng + "\",");
                strBuilder.Append("\"Flat_Bldg_Chi\":\"" + app_addressENT.Flat_Bldg_Chi + "\",");
                strBuilder.Append("\"Flat_Bldg_Eng\":\"" + app_addressENT.Flat_Bldg_Eng + "\",");
                strBuilder.Append("\"FullAdd_Chi\":\"" + app_addressENT.FullAdd_Chi + "\",");
                strBuilder.Append("\"FullAdd_Eng\":\"" + app_addressENT.FullAdd_Eng + "\",");
                strBuilder.Append("\"PostalCode\":\"" + app_addressENT.PostalCode + "\",");
                strBuilder.Append("\"Province_Chi\":\"" + app_addressENT.Province_Chi + "\",");
                strBuilder.Append("\"Province_Eng\":\"" + app_addressENT.Province_Eng + "\",");
                strBuilder.Append("\"Street_Rd_Chi\":\"" + app_addressENT.Street_Rd_Chi + "\",");
                strBuilder.Append("\"Street_Rd_Eng\":\"" + app_addressENT.Street_Rd_Eng + "\",");
                strBuilder.Append("\"AreaUID\":\"");
                if (app_addressENT.mtblCountry != null)
                {
                    if (app_addressENT.mtblCountry.AreaUID != null)
                    {
                        strBuilder.Append(app_addressENT.mtblCountry.AreaUID);
                    }
                    else
                    { strBuilder.Append(0); }
                }
                strBuilder.Append("\"");
                strBuilder.Append("}");
            }
            return strBuilder.ToString();
        }
        private string GetStrFromTblAddress(tblAddress ent)
        {
            StringBuilder strBuilder = new StringBuilder();
            if (ent != null)
            {
                strBuilder.Append("{");
                strBuilder.Append("\"Area_Chi\":\"" + ent.Area_Chi + "\",");
                strBuilder.Append("\"Area_Eng\":\"" + ent.Area_Eng + "\",");
                strBuilder.Append("\"City_Chi\":\"" + ent.City_Chi + "\",");
                strBuilder.Append("\"City_Eng\":\"" + ent.City_Eng + "\",");
                strBuilder.Append("\"CountryUID\":\"" + ent.CountryUID + "\",");
                strBuilder.Append("\"District_Chi\":\"" + ent.District_Chi + "\",");
                strBuilder.Append("\"District_Eng\":\"" + ent.District_Eng + "\",");
                strBuilder.Append("\"Flat_Bldg_Chi\":\"" + ent.Flat_Bldg_Chi + "\",");
                strBuilder.Append("\"Flat_Bldg_Eng\":\"" + ent.Flat_Bldg_Eng + "\",");
                strBuilder.Append("\"FullAdd_Chi\":\"" + ent.FullAdd_Chi + "\",");
                strBuilder.Append("\"FullAdd_Eng\":\"" + ent.FullAdd_Eng + "\",");
                strBuilder.Append("\"PostalCode\":\"" + ent.PostalCode + "\",");
                strBuilder.Append("\"Province_Chi\":\"" + ent.Province_Chi + "\",");
                strBuilder.Append("\"Province_Eng\":\"" + ent.Province_Eng + "\",");
                strBuilder.Append("\"Street_Rd_Chi\":\"" + ent.Street_Rd_Chi + "\",");
                strBuilder.Append("\"Street_Rd_Eng\":\"" + ent.Street_Rd_Eng + "\",");
                strBuilder.Append("\"AreaUID\":\"");
                if (ent.mtblCountry != null)
                {
                    if (ent.mtblCountry.AreaUID != null)
                    {
                        strBuilder.Append(ent.mtblCountry.AreaUID);
                    }
                    else
                    { strBuilder.Append(0); }
                }
                strBuilder.Append("\"");
                strBuilder.Append("}");
            }
            
            return strBuilder.ToString();
        }
        private tblAddress GettblAddressENT(int addressUID)
        {

            if (addressUID > 0)
            {
                tblAddress addressENT = new tblAddress();
                tblAddressBLL addressBLL = new tblAddressBLL();
                addressENT.AddressUID = addressUID;
                return addressBLL.GetEntity(addressENT);
            }
            return null;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}